/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gobygang.server;

import java.util.Date;
import java.util.logging.Logger;

import com.gobygang.client.GobygangService;
import com.gobygang.shared.ChitInfo;
import com.gobygang.shared.ContactInfo;
import com.gobygang.shared.LoginInfo;
import com.gobygang.shared.RouteInfo;
import com.gobygang.shared.TaxiGangInfo;
import com.gobygang.shared.TaxiGangInfo.LeaveResult;
import com.gobygang.shared.TaxiGanguserInfo;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 *
 * @author hall
 */

public class GobygangServiceImpl extends RemoteServiceServlet implements GobygangService {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger log = Logger.getLogger(GobygangServiceImpl.class.getName());

	private TaxiGangManager taxiGangManager = new TaxiGangManager();

	public TaxiGangInfo[] getTaxiGangs(TaxiGangInfo taxiganginfo){
		return getTaxiGangs(taxiganginfo, new Date());
	}

	public TaxiGangInfo[] getTaxiGangs(TaxiGangInfo taxiganginfo, Date now){
		return taxiGangManager.getTaxiGangs(taxiganginfo, now);


	} // end method getTaxiGangs

	public TaxiGangInfo[] getMyTaxiGangs(LoginInfo loginInfo){
		//Log.info("TaxiGangServiceImpl:getMyTaxiGangs "+loginInfo.getId()+" "+loginInfo.getLocale());
		return getMyTaxiGangs(loginInfo, new Date());
	}
	public TaxiGangInfo[] getMyTaxiGangs(LoginInfo loginInfo, Date now){

		return taxiGangManager.getMyTaxiGangs(loginInfo);

	} // end method getTaxiGangs

	public void createWishRoute(TaxiGangInfo taxiganginfo, LoginInfo loginInfo) {
		taxiGangManager.createWishRoute(taxiganginfo, loginInfo);
	}

	public void cancelWishRoute(String wishRouteId) {
		taxiGangManager.cancelWishRoute(wishRouteId);
	}



	public TaxiGangInfo createTaxiGang(TaxiGangInfo taxiganginfo, LoginInfo loginInfo, ChitInfo chitInfo) {
		
		TaxiGangInfo newTaxiganginfo = taxiGangManager.getTaxiGang(taxiganginfo, loginInfo, chitInfo);
		newTaxiganginfo.setInvitationMessage(taxiganginfo.getInvitationMessage());

		return newTaxiganginfo;
	}


	public void joinTaxiGang(TaxiGangInfo taxiganginfo, LoginInfo loginInfo, ChitInfo chitInfo) {
		taxiGangManager.joinTaxiGang(taxiganginfo,loginInfo, chitInfo);

	}

	public LeaveResult leaveTaxiGang(TaxiGangInfo taxiganginfo, LoginInfo loginInfo) {
		return taxiGangManager.leaveTaxiGang(taxiganginfo,loginInfo);

	}

	public void award(TaxiGangInfo taxiganginfo, LoginInfo loginInfo) {
		taxiGangManager.award(taxiganginfo,loginInfo.getId());
	}


	public TaxiGanguserInfo getTaxiGangUser(LoginInfo loginInfo){
		//Log.debug("TaxiGangService:getTaxiGangUser:id="+loginInfo.getId());
		return taxiGangManager.getTaxiGangUser(loginInfo);
	}


//	public Collection<TaxiGang> getTaxiGangs(TaxiGanguser user){
//		return taxiGangManager.getTaxiGangs(user);
//	}

	public void addMessage(TaxiGangInfo taxiganginfo, LoginInfo loginInfo, String message ) {
		taxiGangManager.addMessage(taxiganginfo, loginInfo, message);
	}
	
	public void deleteAllTaxiGangs(){
		taxiGangManager.deleteAllTaxiGangs();
	}

	public void deleteAllTaxiGangUsers(){
		taxiGangManager.deleteAllTaxiGangUsers();
	}

	public void deleteTaxiGang(TaxiGangInfo taxiganginfo){
		taxiGangManager.deleteTaxiGang(taxiganginfo);
	}

	public void logInfo(String infoMessage){
		//Log.info(infoMessage);
	}
	
		
	public void runCron(String cron){
		//TODO
	}
	
	
//	public double getAffinity(TaxiGangInfo taxiganginfo1, TaxiGangInfo taxiganginfo2){
//
//		double affinity =  	Math.abs(taxiganginfo1.getStartLat() - taxiganginfo2.getStartLat());
//		affinity +=  		Math.abs(taxiganginfo1.getStartLng() - taxiganginfo2.getStartLng());
//		affinity +=  		Math.abs(taxiganginfo1.getDestinationLat() - taxiganginfo2.getDestinationLat());
//		affinity +=  		Math.abs(taxiganginfo1.getDestinationLng() - taxiganginfo2.getDestinationLng());
//		return affinity;
//	}

//	@Override
//	public BaseModel[] crudAdmin(Crud crud, BaseModel bm) {
//		return 	taxiGangManager.crudAdmin(crud, bm);
//	}


//	@Override
//	public TaxiCompanyInfo[] getTaxiCompanies(String postalCode){
//		return 	taxiGangManager.getTaxiCompanies(postalCode);		
//	}

	@Override
	public TaxiGangInfo getInvitation(String invitation) {
		//Log.info("TaxiGangServiceImpl:getInvitation "+invitation);
		return taxiGangManager.getInvitation(invitation);
	}

	@Override
	public RouteInfo getPartner(String partner) {
		// TODO Auto-generated method stub
		return taxiGangManager.getPartner(partner);
	}

	@Override
	public RouteInfo getRoute(String ref) {
		// TODO Auto-generated method stub
		return taxiGangManager.getRoute(ref);
	}

	@Override
	public void createPartner(RouteInfo partnerInfo) {
		taxiGangManager.createPartner(partnerInfo);
		
	}

	@Override
	public void sendContactInfo(ContactInfo info) {
		Mailer.getInstance().sendContactInfo(info);
		
	}



}
